

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>TensorFlow安装 &mdash; 简单粗暴TensorFlow 0.3 beta 文档</title>
  

  
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="索引"
              href="../genindex.html"/>
        <link rel="search" title="搜索" href="../search.html"/>
    <link rel="top" title="简单粗暴TensorFlow 0.3 beta 文档" href="../index.html"/>
        <link rel="next" title="TensorFlow基础" href="basic.html"/>
        <link rel="prev" title="前言" href="preface.html"/> 

  
  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../index.html" class="icon icon-home"> 简单粗暴TensorFlow
          

          
          </a>

          
            
            
              <div class="version">
                0.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="preface.html">前言</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">TensorFlow安装</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#id1">简易安装</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id3">正式安装</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id4">安装前的环境配置</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id7">安装</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#id9">第一个程序</a></li>
<li class="toctree-l2"><a class="reference internal" href="#id12">升级到新版本</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="basic.html">TensorFlow基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="models.html">TensorFlow模型</a></li>
<li class="toctree-l1"><a class="reference internal" href="extended.html">TensorFlow扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="static.html">附录：静态的TensorFlow</a></li>
</ul>
<p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../en/preface.html">Preface</a></li>
<li class="toctree-l1"><a class="reference internal" href="../en/installation.html">TensorFlow Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../en/basic.html">TensorFlow Basic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../en/models.html">TensorFlow Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../en/extended.html">TensorFlow Extensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../en/static.html">Appendix: Static TensorFlow</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">简单粗暴TensorFlow</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
      <li>TensorFlow安装</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/zh/installation.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="tensorflow">
<h1>TensorFlow安装<a class="headerlink" href="#tensorflow" title="永久链接至标题">¶</a></h1>
<p>TensorFlow的最新安装步骤可参考官方网站上的说明（<a class="reference external" href="https://tensorflow.google.cn/install">https://tensorflow.google.cn/install</a>）。TensorFlow支持Python、Java、Go、C等多种编程语言以及Windows、OSX、Linux等多种操作系统，此处及后文均以主流的Python语言为准。</p>
<p>以下提供简易安装和正式安装两种途径，供不同层级的读者选用。</p>
<div class="section" id="id1">
<h2>简易安装<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p>如果只是安装一个运行在自己电脑上的，无需GPU的简易环境，不希望在环境配置上花费太多精力，建议按以下步骤安装（以Windows系统为例）：</p>
<ul class="simple">
<li>下载并安装Python集成环境 <a class="reference external" href="https://www.anaconda.com/download/">Anaconda</a> （Python 3.6版本）；</li>
<li>下载并安装Python的IDE <a class="reference external" href="http://www.jetbrains.com/pycharm/">PyCharm</a> （Community版本，或学生可申请Professional版本的 <a class="reference external" href="https://sales.jetbrains.com/hc/zh-cn/articles/207154369">免费授权</a>）；</li>
<li>打开开始菜单中的“Anaconda Prompt”，输入 <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tensorflow</span></code>；</li>
<li>启动PyCharm，新建工程（使用默认python interpreter），在工程内新建一个Python文件。</li>
</ul>
<p>完毕。</p>
</div>
<div class="section" id="id3">
<h2>正式安装<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>该部分包含了更多安装上的细节（如建立conda环境），以及GPU版本TensorFlow的环境配置方法。</p>
<div class="section" id="id4">
<h3>安装前的环境配置<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h3>
<p>正式安装TensorFlow前，需要为其配置合适的环境。步骤如下：</p>
<ol class="arabic simple">
<li>检查自己的电脑是否具有NVIDIA显卡。如有，建议安装GPU版本的TensorFlow，以利用GPU强大的计算加速能力 <a class="footnote-reference" href="#f1" id="id5">[1]</a> ，否则可以安装CPU版本。具体而言，该显卡的CUDA Compute Capability须不低于3.0，可以到 <a class="reference external" href="https://developer.nvidia.com/cuda-gpus/">NVIDIA的官方网站</a> 查询自己所用显卡的CUDA Compute Capability；</li>
<li>安装Python环境。此处建议安装Anaconda，这是一个开源的Python发行版本，提供了一个完整的科学计算环境，包括NumPy、SciPy等常用科学计算库。当然，你有权选择自己喜欢的Python环境。注意截至本手册撰写时，TensorFlow在Windows下的安装仅支持Python 3.X版本；<ul>
<li>安装Anaconda时，可以选择将Anaconda目录添加到系统的PATH中（虽然安装程序不推荐这样做），这样可以直接在命令行环境下使用Anaconda的各项功能。当然，不添加的话也可以使用开始菜单中的Anaconda Prompt进入命令行的Anaconda环境。</li>
</ul>
</li>
<li>（针对GPU版本）安装NVIDIA显卡驱动程序、 <a class="reference external" href="https://developer.nvidia.com/cuda-downloads">CUDA Toolkit</a> 和 <a class="reference external" href="https://developer.nvidia.com/cudnn">cuDNN</a> 。值得注意的事项有：<ul>
<li>建议的顺序是：先安装最新版NVIDIA显卡驱动程序，再安装CUDA（安装时不要选择同时安装驱动），最后安装cuDNN。CUDA附带的显卡驱动程序可能过旧；</li>
<li>在Ubuntu下有一个很简易的驱动安装方法：在系统设置（System Setting）里面选软件与更新（Software &amp; Updates），然后点选Additional Drivers里面的“Using NVIDIA binary driver”选项并点选右下角的“Apply Changes”即可，系统即会自动安装NVIDIA驱动。否则，NVIDIA显卡驱动程序在Linux系统上的安装往往不会一帆风顺，注意在安装前禁用系统自带的开源显卡驱动Nouveau、禁用主板的Secure Boot功能。更详细的指导可以参考 <a class="reference external" href="https://www.linkedin.com/pulse/installing-nvidia-cuda-80-ubuntu-1604-linux-gpu-new-victor/">这篇文章</a> ；</li>
<li>CUDA Toolkit和cuDNN的版本一定要与TensorFlow官方网站安装说明的版本一致，注意官方网站安装说明里要求安装的版本可能并非最新版本；</li>
<li>cuDNN的安装方式比较特殊，你需要手动将下载的安装包复制到CUDA的安装目录下。</li>
</ul>
</li>
</ol>
</div>
<div class="section" id="id7">
<h3>安装<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h3>
<p>在Anaconda环境下的安装过程如下（以Windows系统为例）：</p>
<ol class="arabic simple">
<li>新建一个叫做 <code class="docutils literal"><span class="pre">tensorflow</span></code> 的conda环境</li>
</ol>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">create</span> <span class="o">-</span><span class="n">n</span> <span class="n">tensorflow</span> <span class="n">python</span><span class="o">=</span><span class="n">X</span><span class="o">.</span><span class="n">X</span> <span class="c1"># 注意这里的X.X填写自己Python环境的版本，例如3.6</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li>激活环境</li>
</ol>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">activate</span> <span class="n">tensorflow</span>
</pre></div>
</div>
<ol class="arabic simple" start="3">
<li>使用pip安装TensorFlow</li>
</ol>
<p>安装CPU版本</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span>
</pre></div>
</div>
<p>安装GPU版本</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span><span class="o">-</span><span class="n">gpu</span>
</pre></div>
</div>
<p>如有需要，也可以安装TensorFlow的Nightly版本，该版本较之于正式版本会具有一些最新的特性（例如在TensorFlow 1.8版本以前，本手册主要使用的Eager Execution模式只在Nightly版本中提供），然而稳定度可能稍弱。在一个新的虚拟环境里运行 <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tf-nightly</span></code> （CPU版本）或 <code class="docutils literal"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">tf-nightly-gpu</span></code> （GPU版本）即可。注意，若安装GPU版本，其往往要求安装比正式版要求中更新的CUDA和cuDNN。好在CUDA和cuDNN的不同版本是可以共存的。</p>
<p>如果使用pip命令安装速度较慢，可以尝试 <a class="reference external" href="https://mirrors.tuna.tsinghua.edu.cn/help/tensorflow/">清华大学开源软件镜像站的TensorFlow镜像</a>。</p>
</div>
</div>
<div class="section" id="id9">
<h2>第一个程序<a class="headerlink" href="#id9" title="永久链接至标题">¶</a></h2>
<p>安装完毕后，我们来编写一个简单的程序来验证安装。</p>
<p>在命令行下输入 <code class="docutils literal"><span class="pre">activate</span> <span class="pre">tensorflow</span></code> 进入之前建立的安装有TensorFlow的conda环境，再输入 <code class="docutils literal"><span class="pre">python</span></code> 进入Python环境，逐行输入以下代码：</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tensorflow</span> <span class="kn">as</span> <span class="nn">tf</span>
<span class="n">tf</span><span class="o">.</span><span class="n">enable_eager_execution</span><span class="p">()</span>

<span class="n">A</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]])</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">([[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]])</span>
<span class="n">C</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="n">C</span><span class="p">)</span>
</pre></div>
</div>
<p>如果能够最终输出:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">tf</span><span class="o">.</span><span class="n">Tensor</span><span class="p">(</span>
<span class="p">[[</span><span class="mi">19</span> <span class="mi">22</span><span class="p">]</span>
<span class="p">[</span><span class="mi">43</span> <span class="mi">50</span><span class="p">]],</span> <span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">int32</span><span class="p">)</span>
</pre></div>
</div>
<p>说明TensorFlow已安装成功。运行途中可能会输出一些TensorFlow的提示信息，属于正常现象。</p>
<p>此处使用的是Python语言，关于Python语言的入门教程可以参考 <a class="reference external" href="http://www.runoob.com/python3/python3-tutorial.html">http://www.runoob.com/python3/python3-tutorial.html</a> 或 <a class="reference external" href="https://www.liaoxuefeng.com">https://www.liaoxuefeng.com</a> ，本手册之后将默认读者拥有Python语言的基本知识。不用紧张，Python语言易于上手，而TensorFlow本身也不会用到Python语言的太多高级特性。关于Python的IDE，建议使用 <a class="reference external" href="http://www.jetbrains.com/pycharm/">PyCharm</a> 。如果你是学生并有.edu结尾的邮箱的话，可以在 <a class="reference external" href="http://www.jetbrains.com/student/">这里</a> 申请免费的授权。如果没有，也可以下载社区版本的PyCharm，主要功能差别不大。</p>
<table class="docutils footnote" frame="void" id="f1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id5">[1]</a></td><td>GPU加速的效果与模型类型和GPU的性能有关，如果CPU性能较高，但GPU仅有入门级的性能，其实速度提升不大，大概1-2倍。不过如果GPU性能强大的话（例如，本手册写作时，NVIDIA GeForce GTX 1080 Ti或NVIDIA GeForce TITAN系列是市场上性能较强大的显卡型号），对于特定模型，十几倍甚至更高的加速效果也是可以达到的。同时，GPU的加速效果与任务本身也有关。入门级的TensorFlow模型往往不需要太高的计算性能，CPU版本的TensorFlow足以胜任，因此可以待到掌握TensorFlow的基本知识后，再决定是否购入更高级的GPU以得到更快的训练速度。</td></tr>
</tbody>
</table>
</div>
<div class="section" id="id12">
<h2>升级到新版本<a class="headerlink" href="#id12" title="永久链接至标题">¶</a></h2>
<p>TensorFlow的版本频繁更新，如果希望升级当前的TensorFlow版本，请进入安装有TensorFlow的conda环境下输入</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span> <span class="o">--</span><span class="n">upgrade</span>
</pre></div>
</div>
<p>如果你想安装特定版本的TensorFlow，请输入</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="n">tensorflow</span><span class="o">==</span><span class="mf">1.8</span><span class="o">.</span><span class="mi">0</span>   <span class="c1"># 1.8.0为指定版本</span>
</pre></div>
</div>
<p>升级有风险，可能出现升级后TensorFlow导入出错的情况，比较简单的方式是删除当前conda环境后重新安装一遍。以下conda命令可能会有用</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="nb">list</span>                                              <span class="c1"># 列出当前conda环境下所有package及版本</span>
<span class="n">conda</span> <span class="n">env</span> <span class="nb">list</span>                                          <span class="c1"># 列出所有conda环境</span>
<span class="n">conda</span> <span class="n">create</span> <span class="o">--</span><span class="n">name</span> <span class="n">new_env_name</span> <span class="o">--</span><span class="n">clone</span> <span class="n">old_env_name</span>   <span class="c1"># 备份当前conda环境`old_env_name`到`new_env_name`</span>
<span class="n">conda</span> <span class="n">env</span> <span class="n">remove</span> <span class="o">-</span><span class="n">n</span> <span class="n">tensorflow</span>                          <span class="c1"># 删除名为`tensorflow`的conda环境</span>
</pre></div>
</div>
</div>
</div>


           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="basic.html" class="btn btn-neutral float-right" title="TensorFlow基础" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="preface.html" class="btn btn-neutral" title="前言" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Xihan Li（雪麒）.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'0.3 beta',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>
      <script type="text/javascript" src="../_static/translations.js"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>